package com.ITArmy.service.impl;

import com.ITArmy.beans.Order;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.OrderMapper;
import com.ITArmy.service.OrderService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    GetUserSession getUserSession=new GetUserSession();
    @Autowired
    private OrderMapper orderMapper;

    //添加订单信息

    @Override
    public ServerResponse addOrderByBack(Order order) {
        int resultCount=orderMapper.insertSelective(order);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"添加成功","添加失败");
    }

    //遍历订单信息
    @Override
    public List<Order> orderList() {
        return orderMapper.selectAllOrder();
    }

    //修改订单信息
    @Override
    public ServerResponse updateOrder(Order order) {
        int resultCount = orderMapper.updateByPrimaryKeySelective(order);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"修改信息成功","修改信息失败");
    }

    //通过订单号查找订单信息
    @Override
    public ServerResponse findOrderByOId(Long oid) {
        Order order = orderMapper.selectByPrimaryKey(oid);
        if (order != null) {
            return ServerResponse.createBySuccess(order);
        }
        return ServerResponse.createBySuccessMessage("无该订单号信息！");
    }

    //通过用户id查找订单信息
    @Override
    public ServerResponse findOrderByUid(Long uid) {
        List<Order> orders = orderMapper.selectOrderByUid(uid);
        if (orders != null) {
            return ServerResponse.createBySuccess(orders);
        } else {
            return ServerResponse.createByErrorMesage("无该用户订单信息！");
        }
    }

    //通过连载id查找订单信息
    @Override
    public ServerResponse findOrderBySid(Long sid) {
        List<Order> orders = orderMapper.selectOrderBySid(sid);
        if (orders != null) {
            return ServerResponse.createBySuccess(orders);
        } else {
            return ServerResponse.createByErrorMesage("无该连载订单信息！");
        }
    }

    //通过订单号删除订单信息
    @Override
    public ServerResponse deleteOrder(Long oid) {
        return ServerResponse.createBySuccess(orderMapper.deleteByPrimaryKey(oid));
    }

    //批量删除
    @Override
    public ServerResponse deleteManyByIds(String[] ids) {
        int resultCount=orderMapper.deleteManyByIds(ids);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"删除成功","删除失败");
    }

    //通过用户id查找购买连载id
    @Override
    public List<Long> findSidByUid(long uid) {
        return orderMapper.findSidByUid(uid);
    }

    //用户通过购买产生订单信息
    @Override
    public ServerResponse addOrder(HttpServletRequest request,Order order) {
        order.setuId(getUserSession.getUser(request).getuId());
        order.setsTime(new Date());
        int resultCount = orderMapper.insertSelective(order);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"订单生成成功","订单生成失败");
    }

    //用户通过支付宝支付时生成订单
    @Override
    public ServerResponse addOrderWhenAlipay(HttpServletRequest request,Order order) {
        order.setuId(getUserSession.getUser(request).getuId());
        int resultCount = orderMapper.insertSelective(order);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"订单生成成功","订单生成失败");
    }
}
